<?php

$tstamp = time();

// expire the subscriptions
$expired = $DB->fetchAll("SELECT * FROM {$this->config['db_prefix']}isubscribe_users_to_subscriptions WHERE expire_tstamp <= $tstamp");

foreach($expired as $k=>$v){
  $DB->delete(cms_db_prefix().'isubscribe_users_to_subscriptions', "user_id = '{$v['user_id']}' AND subscription_id = '{$v['subscription_id']}' AND expire_tstamp = '{$v['expire_tstamp']}'");
}

// expire and revoke group to user associations that have expired
$expired = $DB->fetchAll("SELECT * FROM {$this->config['db_prefix']}isubscribe_expirations WHERE expire_tstamp <= $tstamp");
foreach($expired as $k=>$v){
  if($DB->countRows(cms_db_prefix().'isubscribe_expirations', "user_id = '{$v['user_id']}' AND group_key = '{$v['group_key']}' AND expire_tstamp > $tstamp")>0){
    // just delete this entry
    $DB->delete(cms_db_prefix().'isubscribe_expirations', "user_id = '{$v['user_id']}' AND group_key = '{$v['group_key']}' AND expire_tstamp = '{$v['expire_tstamp']}'");
  }else{
    // revoke groups
    $DB->delete(cms_db_prefix().'isubscribe_expirations', "user_id = '{$v['user_id']}' AND group_key = '{$v['group_key']}'");
    $DB->delete(cms_db_prefix().'iusers_to_groups', "user_id = '{$v['user_id']}' AND group_key = '{$v['group_key']}'");
  } 
}

// send notifications
$notifications = $DB->fetchAll("SELECT * FROM {$this->config['db_prefix']}isubscribe_notifications");
foreach($notifications as $k=>$v){
  $closingTstamp = (time()+($v['num_days']*86400));
  $usersToNotify = $DB->fetchAll("SELECT iusers.id AS id FROM {$this->config['db_prefix']}iusers iusers JOIN 
    {$this->config['db_prefix']}isubscribe_users_to_subscriptions u2subs ON iusers.id = u2subs.user_id
    WHERE u2subs.expire_tstamp <= {$closingTstamp}");
  foreach($usersToNotify as $key=>$user){
    if($DB->countRows(cms_db_prefix().'isubscribe_notifications_sent', "notification_id = '{$v['id']}' AND user_id = '{$user['id']}'") > 0)
      // already sent
      continue;
    
    // now we are dealing with a due notification that has not been sent
    $uData = $DB->fetchRow("SELECT * FROM {$this->config['db_prefix']}iusers WHERE id = '{$user['id']}'");
    $subData = $DB->fetchRow("SELECT * FROM {$this->config['db_prefix']}isubscribe_subscriptions WHERE id = '{$v['subscription_id']}'");
    
    if($subData['is_active']==0 || $subData['is_deleted']==1) $renewLink = $Antz->getUrlById($this->GetConfig('advertisement_id'));
    else $renewLink = $Antz->addParamsToUrl($Antz->getUrlById($this->GetConfig('purchase_subscription_id')), array('subscription'=>$subData['keyname']));
    
    echo $renewLink.'<br />';
    
    $this->smarty->assign('uData', $uData);
    $this->smarty->assign('renewLink', $renewLink);
    $this->smarty->assign('subData', $subData);
    
    $emailContent = $this->ProcessTemplateFromData($v['content']);
    
    $this->smarty->assign('ISubscribe_EmailContent', $emailContent);
    
    $emailContent = $this->ProcessTemplate('email_layout.tpl');
    
    $toFrom = array(
      'toEmail' => $CRYPT->decrypt($uData['email_crypt']),
      'toName' => trim($uData['first_name'].' '.$uData['last_name'])
    );
    
    Antz_Email::sendRaw($v['subject'], $emailContent, $toFrom);
    
    $dbParams = array(
      'notification_id' => $v['id'],
      'user_id' => $uData['id'],
      'sent_tstamp' => time()
    );
    $DB->insert(cms_db_prefix().'isubscribe_notifications_sent', $dbParams);
    
    
  }
}





